import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)


export default new Vuex.Store({
    state: {
        sid: '',
        searchKey: '',
        scrollBusy: true, //无限滚动 
        isLog: false,  // 登录状态
        top: false,
        bottom: false,
        topicTitle: false,
        topTitle: '欢迎来到知否社区',
        user: {}
    },
    mutations: {
        // 存储用户数据
        saveUser(state, user) {
            state.user = user
        },
        // 退出
        logOut(state) {
            state.user = {
                UID: 0
            }
        },
        // 图片验证码id
        setSid(state, val) {
            state.sid = val
        },
        // 搜索 关键字
        setSearchKey(state, val) {
            state.searchKey = val
        },
        // 首页无限滚动滚动 状态
        setScrollBusy(state, val) {
            state.scrollBusy = val
        },
        // 触摸状态
        setTouchMove(state, val) {
            if (val) {
                state.top = true
                state.bottom = false
            } else {
                state.top = false
                state.bottom = true
            }
        },
        // 点赞
        addUserLikeArt(state, val) {
            state.user.goodArticle.push(val)
        },
        // 取消赞
        delUserArt(state, index) {
            state.user.goodArticle.splice(index, 1)
        },
        // 关注用户
        addFollow(state, uuid) {
            if (state.user.follow == undefined) {
                state.user.follow = []
            }
            state.user.follow.push(uuid);
        },
        // 取消关注
        delFollow(state, index) {
            state.user.follow.splice(index, 1)
        },
        // 头部是否滚动
        setHeaderScroll(state, val) {
            state.topicTitle = val
        },
        setTopTitle(state, val) {
            state.topTitle = val
        }
    },
    actions: {
        //  点赞
        addUserArt({ commit, state }, tid) {
            // 去重一下加进去
            commit('addUserLikeArt', tid)
        },
        // 取消点赞
        delUserArt({ commit, state }, tid) {
            for (let i = 0; i < state.user.goodArticle.length; i++) {
                if (state.user.goodArticle[i] == tid) {
                    // 删除
                    commit('delUserArt', i)
                }
            }
        },
        // 取消对用户uid的关注
        delUserFollow({ commit, state }, uuid) {
            for (let i = 0; i < state.user.follow.length; i++) {
                if (state.user.follow[i] == uuid) {
                    // 删除
                    commit('delFollow', i)
                }
            }
        },
        // 对用户uid关注
        addUserFollow({ commit, state }, uuid) {
            commit('addFollow', uuid)
        }
    },
    getters: {
        getUser: state => {
            return state.user
        }
    }
})
